<?php
class ClientsController extends AppController {
	var $name = 'Clients';
	var $helpers = array('Html');
	var $components = array('Init', 'Param');
	
	function beforeFilter() {
		if (!$this->Session->read('admin')) {
			$this->redirect('/users/login');
		}
		else {
			$this->Init->startup($this);
			$this->Admin = $this->Init->init();
			
			$this->set('dashboard',0);
			$this->set('content',0);
			$this->set('products',0);
			$this->set('component',1);
			$this->set('system',0);
			
			$this->set('title','Clients Management');
			$this->set('sub_title', $this->Param->getAction($this->params));
  		}
	}
	
	function index(){
		$this->loadModel('Client');
		$clients = $this->Client->find('all', array(
										'order' 	 => 'Client.date desc'
										));
		$this->set('pictures',$clients);
	}
	
	function edit($id = null){
		$this->helpers[] = 'Ck';
		$id = $this->Param->getID($this->params);
		
		if (isset($this->data)) {
			$this->data['Client']['date'] = time();
			$this->data['Client']['status'] = 1;
			if(!empty($_FILES['upload']['name'])) {
				
				App::import('Component', 'Upload');
				$upload = new UploadComponent();
				$upload->setPath('pictures');
				$_error = $upload->checkImage($_FILES['upload']['size']);

				if(empty($_error)) {
					$_file = $upload->upload($_FILES['upload'], $id);
					$this->data['Client']['image'] = $_file;
				}
				else {
					$this->Session->setFlash($_error);
					$this->redirect('clients');
				}
			}
			
			if ($this->Client->save($this->data)) {
				$this->Session->setFlash('Done successfully!');
				$this->redirect('/clients');
			}
			
		}
		else {
			if (isset($id) && ($id > 0)) {
				$this->data = $this->Client->find('first',array(
					'conditions' => array('Client.id' => $id)
				));
			}
		}
	}
	
	
	function del($id = null) {
		$id = $this->Param->getID($this->params);
		 $this->Client->id = $id;
		 $this->Client->delete();
		 $this->Session->setFlash('Done Succesfully!');
		 $this->redirect('/clients');
	}
}
?>